package com.nbgs.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbgs.demo.common.EncryptUtil;
import com.nbgs.demo.common.Result;
import com.nbgs.demo.controller.form.WorkerNews;
import com.nbgs.demo.model.Worker;
import com.nbgs.demo.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@Slf4j
@CrossOrigin("*")
public class WorkerController {
@Autowired
private WorkerService workerService;
    //展示员工的信息
    @GetMapping("showWorkerMessage")
    public Result showAll(@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "10")int pageSize, @RequestParam String workerName,@RequestParam String  workerNum,@RequestParam String workerState){
        log.info("分页展示员工信息{}{}{}{}{}{}",currPage,pageSize,workerName,workerNum,workerState);
        Page<Worker> pageParm = new Page<>();
        pageParm.setCurrent(currPage);
        pageParm.setSize(pageSize);
        pageParm.setTotal(workerService.totalWorker(workerName, workerNum, workerState));
        IPage<Worker> all = workerService.showSumWorker(pageParm,workerName,workerNum,workerState);
        return Result.success(all);
    }
    //新增录入员工信息
    @PostMapping("addWorker")
    public Result addWorker(@RequestBody WorkerNews workerNews){
        log.info("新增员工信息{}",workerNews);
        Worker worker=new Worker();
        worker.setWorkerState("在职");
        worker.setWorkerStartTime(new Date());
        worker.setWorkerPassword(EncryptUtil.encrypt("123456"));
        worker.setWorkerAddress(workerNews.getWorkerAddress());
        worker.setWorkerGender(workerNews.getWorkerGender());
        worker.setWorkerName(workerNews.getWorkerName());
        worker.setWorkerTelNum(workerNews.getWorkerTelNum());
        worker.setWorkerNum(workerNews.getWorkerNum());
        worker.setWorkerIdNum(workerNews.getWorkerIdNum());
        workerService.addWorker(worker);
        workerService.addRoleAndRole(worker.getWorkerId(),workerNews.getRoleName());
        return Result.success();
    }
    //修改员工信息（联系电话和离职时间或者状态）
    @PostMapping("updateWorker")
    public Result updateWorker(@RequestBody Worker worker){
        log.info("修改员工信息{}",worker);
     workerService.updateWorker(worker);
        return Result.success();
    }
}
